<?php
/**
 * Zeed Platform Project
 * Based on Zeed Framework & Zend Framework.
 *
 * LICENSE
 * http://www.zeed.com.cn/license/
 *
 * @category Zeed
 * @package Zeed_ChangeMe
 * @subpackage ChangeMe
 * @copyright Copyright (c) 2010 Zeed Technologies PRC Inc. (http://www.zeed.com.cn)
 * @author Zeed Team (http://blog.zeed.com.cn)
 * @since 2011-10-26
 * @version SVN: $Id$
 */
class Hooks_Notify_AttachmentDeleteConfirmHook
{
    /* 消息类型 */
    protected static $_type = 'attachment_delete_confirm';
    
    /**
     * 添加消息记录 - 资料删除二次确认
     *
     * @param integer $send_userid 发送消息的用户 ID
     * @param integer $to_userid 接收消息的用户 ID
     * @param string $sn 资料 ID，即附件 ID
     * @param string $card_id 资料所属行动 ID
     * @param string $confirm_id 资料删除二次确认队列 ID
     * @return array | boolean
     */
    public static function run($send_userid = 0, $to_userid = 0, $sn = 0, $card_id = 0, $confirm_id = 0)
    {
        $res = array('status' => 0, 'error' => null, 'data' => null);
        
        try {
            $timenow = Util_Timer::getMicrotime();
            
            /* 校验传递参数 */
            if (empty($send_userid)) {
                throw new Zeed_Exception('发送用户 ID 未提供');
            }
            if (empty($to_userid)) {
                throw new Zeed_Exception('接收用户 ID 未提供');
            }
            if (! $sn) {
                throw new Zeed_Exception('资料 ID 未提供');
            }
            
            /* 查询资料内容 */
            $attachment_info = Attachment_Model_Content::instance()->fetchByPK($sn);
            
            /* 获取二次确认队列信息 */
            $confirm_info = Archive_Model_Confirm::instance()->fetchByPK($confirm_id);
            
            /* 组织写入的消息内容 */
            $config_template = Zeed_Config::loadGroup('notifytemplate.attachment_delete_confirm');
            $content = str_replace('{#title#}', $attachment_info[0]['title'], $config_template);
            
            /* 组织扩展信息 */
            $extra = array('attachment_info' => $attachment_info[0], 'confirm_info' => $confirm_info[0], 'card_id' => $card_id);
            
            /* 组织写入数据 */
            $set = array(
                    'type' => self::$_type,
                    'content' => $content,
                    'extra' => json_encode($extra),
                    'send_userid' => (int) $send_userid,
                    'to_userid' => (int) $to_userid,
                    'read_users' => '',
                    'is_del' => 0,
                    'ctime' => $timenow,
                    'mtime' => $timenow
            );
            Trend_Model_Notify::instance()->addForEntity($set);
        } catch (Zeed_Exception $e) {
            $res['status'] = 1;
            $res['error'] = '添加消息失败。' . $e->getMessage();
        }
        
        return json_encode($res);
    }
}

// End ^ Native EOL ^ UTF-8